/*
 * Copyright (C) 2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;

import java.io.IOException;

/**
 * CentrePositionAbility
 *
 * @since 2021-06-21
 */
public class CentrePositionAbility extends Ability implements Component.ClickedListener {
    private DependentLayout mRootView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_centre_position);
        initViews();
    }

    private void initViews() {
        mRootView = (DependentLayout) findComponentById(ResourceTable.Id_dl);
        findComponentById(ResourceTable.Id_showPromptc).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc1).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc2).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc3).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc4).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc5).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc6).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc7).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc8).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc9).setClickedListener(this);
        findComponentById(ResourceTable.Id_showPromptc10).setClickedListener(this);
        findComponentById(ResourceTable.Id_ig_back).setClickedListener(component -> terminateAbility());
    }

    /**
     * showPrompt
     *
     * @param view view
     */
    public void showPrompt(final Component view) {
        try {
            new MaterialTapTargetPrompt.Builder(this, mRootView)
                    .setPrimaryText(ResourceTable.String_fab_centre_title)
                    .setSecondaryText(ResourceTable.String_fab_centre_description)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setMaxTextWidth(ResourceTable.Float_max_prompt_width)
                    .setTarget(view)
                    .show();
        } catch (NotExistException | WrongTypeException | IOException e) {
            String error = e.toString();
        }
    }

    @Override
    public void onClick(Component component) {
        showPrompt(component);
    }
}
